package com.hejia.alauda.entity;

import javax.persistence.Column;

import org.apache.commons.lang3.StringUtils;

//import com.umpay.api.util.StringUtil;

/**
 * 推广访问
 * @author hyr
 *
 */
public class PopularizeVisit extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -259369803857480924L;
	
	/**
	 * 来源ID格式
	 */
	public static final String SOURCE_ID_FORMAT = "P:%s-%s-%s";
	
	/**
	 * 包含下一级来源ID格式
	 */
	public static final String SUB_SOURCE_ID_FORMAT = "P:%s-%s-%s-%s";
	
	/**
	 * 来源ID分割符
	 */
	public static final String SOURCE_ID_SEPARATOR = "-";

	/**
	 * AdID Cookie名称，用于标识用户
	 */
	public static final String AD_ID_COOKIE_NAME = "AdID";

	/**
	 * AdID Cookie过期时间（单位：秒）
	 */
	public static final int AD_ID_COOKIE_EXPIRY = 24 * 60 * 60;
	
	@Column
	private String source; // 访问来源

	@Column
	private String visitorIP; // 访问者IP
	
	@Column
	private long biggerCategoryId; // 大类ID
	
	@Column
	private long categoryId; // 类别ID
	
	@Column
	private long popularizeId; // 推广ID
	
	@Column 
	private String sourceId; // 推广来源ID
	
	@Column 
	private String subSourceId; // 下一级推广来源ID

	@Column
	private String visitUrl; // 访问者URL
	
	@Column
	private String visitorId; // 访问者Id，用于追踪同一IP的不同用户

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}
	
	public String getVisitorIP() {
		return visitorIP;
	}

	public void setVisitorIP(String visitorIP) {
		this.visitorIP = visitorIP;
	}

	public long getBiggerCategoryId() {
		return biggerCategoryId;
	}

	public void setBiggerCategoryId(long biggerCategoryId) {
		this.biggerCategoryId = biggerCategoryId;
	}

	public long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	public long getPopularizeId() {
		return popularizeId;
	}

	public void setPopularizeId(long popularizeId) {
		this.popularizeId = popularizeId;
	}

	public String getSourceId() {
		return sourceId;
	}

	public void setSourceId(String sourceId) {
		this.sourceId = sourceId;
	}
	
	public String getSubSourceId() {
		return subSourceId;
	}

	public void setSubSourceId(String subSourceId) {
		this.subSourceId = subSourceId;
	}
	
	public String getVisitUrl() {
		return visitUrl;
	}

	public void setVisitUrl(String visitUrl) {
		this.visitUrl = visitUrl;
	}
	
	public String getVisitorId() {
		return visitorId;
	}

	public void setVisitorId(String visitorId) {
		this.visitorId = visitorId;
	}
	
	/**
	 * 格式化来源ID
	 * @param biggerCategoryId	大类ID
	 * @param categoryId		类别ID
	 * @param popularizeId		推广ID
	 * @return
	 */
	public static String formatSourceId(long biggerCategoryId, long categoryId, long popularizeId){
		return String.format(SOURCE_ID_FORMAT, biggerCategoryId, categoryId, popularizeId);
	}
	
	/**
	 * 格式化来源ID
	 * @param biggerCategoryId	大类ID
	 * @param categoryId		类别ID
	 * @param popularizeId		推广ID
	 * @return subSourceId		下一级推广来源ID
	 */
	public static String formatSourceId(long biggerCategoryId, long categoryId, long popularizeId, String subSourceId){
		if(StringUtils.isEmpty(subSourceId)){
			return String.format(SOURCE_ID_FORMAT, biggerCategoryId, categoryId, popularizeId);
		}else{
			return String.format(SUB_SOURCE_ID_FORMAT, biggerCategoryId, categoryId, popularizeId, subSourceId);
		}
	}

	/**
	 * 解析下一级来源ID
	 * @param sourceId
	 * @return	下一级来源ID
	 */
	public static String parseSubSourceId(String sourceId) {
		if(StringUtils.isEmpty(sourceId)){
			return "";
		}
		String[] items = sourceId.split(SOURCE_ID_SEPARATOR);
		if(items.length < 4){
			return "";
		}
		return items[3];
	}

	/**
	 * 解析父级来源ID
	 * @param sourceId 来源ID
	 * @return	父级来源ID
	 */
	public static String parseParentSourceId(String sourceId) {
		int index = sourceId.lastIndexOf("-");
		return sourceId.substring(0, index);
	}
	
}
